home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / pxewin.zip / BROWSE.CPP < prev    next >
Text File  |  1992-02-27  |  10KB  |  378 lines

  1. // PXEWIN - (C) Copyright 1992 by Beam Engineering, INC.
  2.  
  3. // BROWSE.CPP //
  4.  
  5. // Contents ----------------------------------------------------------------
  6. //
  7. //    This module contains members for the Browser class.
  8. //
  9. // End ---------------------------------------------------------------------
  10.  
  11. // External Reference Name for this Header ---------------------------------
  12.  
  13. #ifndef BROWSE_CPP
  14.     #define BROWSE_CPP
  15.  
  16. // End ---------------------------------------------------------------------
  17.  
  18. // Interface Dependencies --------------------------------------------------
  19.  
  20. #ifndef BROWSE_HPP
  21.     #include "browse.hpp"
  22. #endif // BROWSE_HPP //
  23.  
  24. // End ---------------------------------------------------------------------
  25.  
  26. __link(RegWindow)
  27.  
  28. // member Browser //
  29.  
  30. Browser::Browser(PTWindowsObject AParent,int):TWindow(AParent,"")
  31. {
  32.     my_display = NULL;
  33.     Scroller = NULL;
  34.     name = new char[MAXPATH];
  35.  
  36.     // Set the X scroll position to it's default.
  37.  
  38.     XScroll = 0;
  39.  
  40.     // Enable the keyboard so the children can inherit this property
  41.  
  42.     EnableKBHandler();
  43. }
  44.  
  45. // Summary -----------------------------------------------------------------
  46. //
  47. //    Creates a browser window for browsing a Paradox database.
  48. //
  49. // Parameters
  50. //
  51. //      AParent.  This would be the MDI frame window.
  52. //
  53. //    The int is for the window count since each window object is put into
  54. //    a linked list.
  55. //
  56. // Functional Description
  57. //
  58. //    1.  NULL out the display and scroller objects so they will not be
  59. //        deleted before they are created.
  60. //
  61. //      2.  Create a new name buffer for the database name.
  62. //
  63. //    3.  Set the horizontal scroll position to zero by default.  If the
  64. //        window is to be constructed from a stream then this value will
  65. //        be changed when the reader is called.
  66. //
  67. // End ---------------------------------------------------------------------
  68.  
  69. // destructor Browser //
  70.  
  71. inline Browser::~Browser()
  72. {
  73.     delete my_display;
  74.     my_display = NULL;
  75.     delete name;
  76.     name = NULL;
  77.     delete Scroller;
  78.     Scroller = NULL;
  79.  
  80.     // Dispatch a message to the BrowserFrame that you have killed
  81.     // yourself.
  82.  
  83.     SendMessage(Parent->HWindow,WM_CHKCHILD,0,0);
  84. }
  85.  
  86. // Summary -----------------------------------------------------------------
  87. //
  88. //    Delete database name and tell the BrowserFrame to check its children.
  89. //
  90. // End ---------------------------------------------------------------------
  91.  
  92. // member GetDB //
  93.  
  94. int Browser::GetDB()
  95. {
  96.     strcpy(name,"*.db");            /* Construct a file mask for
  97.                            the file dialog */
  98.  
  99.     // Execute file dialog
  100.  
  101.     if(GetApplication()->ExecDialog(
  102.         new TFileDialog(this,SD_FILEOPEN,
  103.         name)) == IDOK)
  104.     {
  105.         flag = EXISTS;
  106.  
  107.         // Store a copy of the database name to the window header
  108.  
  109.         Title = _fstrdup(name);
  110.         if(*Title == NULL)
  111.             flag = NOTEXISTS;
  112.     }
  113.     else
  114.         flag = NOTEXISTS;
  115.  
  116.     // If database does not exist, NULL out db and return
  117.  
  118.     if(flag == NOTEXISTS)
  119.         return flag;
  120.  
  121.     // Get the display for the database setup.
  122.  
  123.     my_display = new DBDISPLAY;
  124.     my_display->SetUp(this);
  125.     if(my_display->RetPXError() != PXSUCCESS)
  126.         flag = NOTEXISTS;
  127.  
  128.     // Set up scroll bars.
  129.  
  130.     Attr.Style |= WS_VSCROLL | WS_HSCROLL;
  131.  
  132.     // Point the Scroller to the custom database scroller.  Vertical
  133.     // scroll will be redifined.
  134.  
  135.     Scroller = new PXScroller(this,8,15,80,60);
  136.  
  137.     return flag;
  138. }
  139.  
  140. // Summary -----------------------------------------------------------------
  141. //
  142. //    Sets up the Browser for displaying a database.
  143. //
  144. // Return Value
  145. //
  146. //    Returns flag which indicates whether the database exists of not.
  147. //
  148. // Functional Description
  149. //
  150. //    1.  Construct a file name mask.
  151. //
  152. //    2.  Call the File dialog box.  If file exists, set flag to EXISTS.
  153. //      Save a copy of the file name to the Title of the window data member.
  154. //    If the Title is NULL or the file dialog does not return IDOK then
  155. //    set existance flag to NOTEXIST.
  156. //
  157. //    3.  If flag is set to NOTEXIST, return to BrowserFrame.
  158. //
  159. //    4.  Set up the database.
  160. //
  161. //    5.  If an error occured in the set up, display the error, set the
  162. //    flag to NOTEXIST and return to the MDI.
  163. //
  164. //    6.  Set the styles for the scroll bars and construct the Scroller.
  165. //
  166. // End ---------------------------------------------------------------------
  167.  
  168. // member SetupWindow //
  169.  
  170. void Browser::SetupWindow()
  171. {
  172.     int sum;                /* This is the accumulated
  173.                            length of all the list
  174.                            boxes */
  175.  
  176.     TWindow::SetupWindow();
  177.  
  178.     // You want to set the x scroller range depending on how many
  179.     // total characters you have in all the fields.  The y
  180.     // scroller range is determined by how many records you have
  181.     // in the database.
  182.  
  183.     my_display->my_table->NumRecs();
  184.  
  185.     sum = my_display->RetSum();
  186.  
  187.     Scroller->SetRange((int)(sum*my_display->RetCharWidth()/
  188.         Scroller->XUnit),my_display->EngDataPtr->num_recs - 1);
  189.  
  190.     // Get the data from the database and fill up list boxes
  191.     // with it and select the first entrie.
  192.  
  193.     my_display->FillBoxes(my_display->top_rec);
  194.  
  195.     // If the value of XScroll is not zero, then do a ScrollBy.  This is
  196.     // so you can set the initial position of the scroll to what has
  197.     // been saved on the desktop stream.
  198.  
  199.     if(XScroll)
  200.     {
  201.         // Check and see if your still in field range first.  Some
  202.         // one could have changed the database structure since you
  203.         // last saved the desktop.
  204.  
  205.         if(XScroll > sum)
  206.             XScroll = sum;
  207.         Scroller->ScrollBy(XScroll,0);
  208.     }
  209. }
  210.  
  211. // Summary -----------------------------------------------------------------
  212. //
  213. //    Sets up browser window
  214. //
  215. // Parameters
  216. //
  217. //    None.
  218. //
  219. // Return Value
  220. //
  221. //    None.
  222. //
  223. // Description
  224. //
  225. //      Calls TWindows set up.  Sets the range on the scroll bar to
  226. //    conform with the database ranges and fills the list boxes with
  227. //    the data.  If XScroll is not zero then a scroll position has been
  228. //    read off the stream so the scroller is set to that position.  The
  229. //    DBDISPLAY class sets the vertical scroll position.
  230. //
  231. // End ---------------------------------------------------------------------
  232.  
  233. // member read of Browser //
  234.  
  235. inline Pvoid Browser::read(Ripstream)
  236. {
  237.     return this;
  238. }
  239.  
  240. // member write of Browser //
  241.  
  242. inline void Browser::write(Ropstream)
  243. {
  244.  
  245. }
  246.  
  247. // member ReadChildren of Browser //
  248.  
  249. inline int Browser::ReadChildren(Ripstream is)
  250. {
  251.     int error;                /* Non-zero if error */
  252.  
  253.     // Get the database name off the stream.
  254.  
  255.     name = is.freadString();
  256.  
  257.     // Get the window attribs off the stream
  258.  
  259.     is >> (long)Attr.Style >> Attr.X >> Attr.Y >> Attr.W >> Attr.H;
  260.  
  261.     // Construct a new display from the stream
  262.  
  263.     is >> my_display;
  264.  
  265.     // Save name to the window title
  266.  
  267.     Title = _fstrdup(name);
  268.  
  269.     // Set up the display.  If display fails, bail out.
  270.  
  271.     error = my_display->SetUp(this);
  272.     if(error)
  273.         return error;
  274.  
  275.     // Point the Scroller to the custom database scroller.  Vertical
  276.     // scroll will be redifined.
  277.  
  278.     Scroller = new PXScroller(this,8,15,80,60);
  279.  
  280.     // Initialize Scroller to correct position
  281.  
  282.     Scroller->YPos = my_display->RetCurRec() - 1;
  283.     is >> XScroll;
  284.     return PXSUCCESS;
  285. }
  286.  
  287. // Summary -----------------------------------------------------------------
  288. //
  289. //    Child reads data off the stream.
  290. //
  291. // Parameters
  292. //
  293. //    is.  The input stream.
  294. //
  295. // Functional Description
  296. //
  297. //      1.  Create a name buffer and read the string for the database name
  298. //        off the stream.
  299. //
  300. //    2.  Read the window attributes from the stream.
  301. //
  302. //    3.  Construct a new display from the stream.
  303. //
  304. //    4.  The window Title is the name of the database.
  305. //
  306. //    5.  Set the display up.
  307. //
  308. //     6.  Make a scroller.  Set the vertical scroll position to the current
  309. //        record.  Set the horizontal scroll position.
  310. //
  311. // End ---------------------------------------------------------------------
  312.  
  313. // member WriteChildren of Browser //
  314.  
  315. inline void Browser::WriteChildren(Ropstream os)
  316. {
  317.     os.fwriteString(name);
  318.     os << (long)Attr.Style << Attr.X << Attr.Y << Attr.W << Attr.H;
  319.     os << my_display;
  320.     os << Scroller->XPos;
  321. }
  322.  
  323. // Summary -----------------------------------------------------------------
  324. //
  325. //    Writes this child to the stream.
  326. //
  327. // Parameters
  328. //
  329. //    os.  This is the stream output.
  330. //
  331. // Functional Description
  332. //
  333. //    1.  Write the name to the stream.
  334. //
  335. //    2.  Save the window Attribs to the stream.
  336. //
  337. //    3.  Save the display to the stream.
  338. //
  339. //     4.  Save the current scroller position to the stream.
  340. //
  341. // End ---------------------------------------------------------------------
  342.  
  343. // member build of Browser //
  344.  
  345. PTStreamable Browser::build()
  346. {
  347.     return new Browser(streamableInit);
  348. }
  349.  
  350. TStreamableClass RegBrowser("Browser",Browser::build,
  351.     __DELTA(Browser));
  352.  
  353. // Description -------------------------------------------------------------
  354. //
  355. //    When the streamable constructor is called, TStreamable dispatches
  356. //    the build member to construct the object.  To do this, it must
  357. //    know where to find this member functions for the specific class.
  358. //    This is the reason for the stream registration.
  359. //
  360. // End ---------------------------------------------------------------------
  361.  
  362. // member SetScroller of Browser //
  363.  
  364. void Browser::SetScroller()
  365. {
  366.     Scroller->YPos = my_display->RetCurRec() - 1;
  367.     Scroller->EndView();
  368. }
  369.  
  370. // Summary -----------------------------------------------------------------
  371. //
  372. //    Sets the scroller position to coorespond to the current position in
  373. //    the database.  Sets the YPos to the currently selected record and
  374. //    calls EndView to update the scroll position.
  375. //
  376. // End ---------------------------------------------------------------------
  377.  
  378. #endif // BROWSE_CPP //